#include "kernel_operator.h"

constexpr int32_t TOTAL_LENGTH = 8 * 2048;                            // total length of data
constexpr int32_t USE_CORE_NUM = 8;                                   // num of core used
constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM;         // length computed of each core
constexpr int32_t TILE_NUM = 8;                                       // split data into 8 tiles for each core
constexpr int32_t BUFFER_NUM = 2;                                     // tensor num for each queue
constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM; // separate to 2 parts, due to double buffer


class KernelHardswish {
public:
    __aicore__ inline KernelHardswish() {}
    __aicore__ inline void Init(GM_ADDR x,GM_ADDR y)
    {
        xGm.SetGlobalBuffer((__gm__ half *)x + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        yGm.SetGlobalBuffer((__gm__ half *)y + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, TILE_LENGTH * sizeof(half));
        pipe.InitBuffer(outQueueY, BUFFER_NUM, TILE_LENGTH * sizeof(half));
        pipe.InitBuffer(tmpBuf0,TILE_LENGTH* sizeof(half));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = TILE_NUM * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }
private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<half> xLocal = inQueueX.AllocTensor<half>();
        AscendC::DataCopy(xLocal, xGm[progress * TILE_LENGTH], TILE_LENGTH);
        inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<half> xLocal = inQueueX.DeQue<half>();
        AscendC::LocalTensor<half> yLocal = outQueueY.AllocTensor<half>();
        AscendC::LocalTensor<half> tmp0 = tmpBuf0.Get<half>();
        AscendC::Adds(yLocal, xLocal, (__fp16)3.0f,TILE_LENGTH);
        AscendC::Maxs(yLocal, yLocal, (__fp16)0.0f, TILE_LENGTH);
        AscendC::Mins(yLocal, yLocal, (__fp16)6.0f,TILE_LENGTH);
        AscendC::Duplicate<half>(tmp0,(__fp16)6.0, TILE_LENGTH);
        AscendC::Div(yLocal, yLocal,tmp0,TILE_LENGTH);
        AscendC::Mul(yLocal,xLocal, yLocal,TILE_LENGTH);
        outQueueY.EnQue<half>(yLocal);
        inQueueX.FreeTensor(xLocal);
        tmpBuf0.FreeTensor(tmp0);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<half> yLocal = outQueueY.DeQue<half>();
        AscendC::DataCopy(yGm[progress * TILE_LENGTH], yLocal, TILE_LENGTH);
        outQueueY.FreeTensor(yLocal);
    }
private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueX, inQueueY;    
    AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> outQueueY;
    AscendC::GlobalTensor<half> xGm;
    AscendC::GlobalTensor<half> yGm;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> tmpBuf0;
};
__global__ __aicore__ void Hardswish_custom(GM_ADDR x, GM_ADDR y)
{
    KernelHardswish kernelHardswish;
    kernelHardswish.Init(x, y);
    kernelHardswish.Process();
}

void Hardswish_custom_do(uint32_t blockDim, void *stream, uint8_t *x, uint8_t *y)
{
    Hardswish_custom<<<blockDim,nullptr,stream>>>(x, y);
}
